{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=\"top\"></a><img src=\"source/SpinalHDL.png\" alt=\"SpinalHDL based on Scala\" style=\"width:320px;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "  Before running Spinal HDL code, be sure to load Spinal HDL Libraries  \n",
    "**Note** : This may be a little slow when the first time load, please wait a moment to download Lib from remote.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val path = System.getProperty(\"user.dir\") + \"/source/load-spinal.sc\"\n",
    "interp.load.module(ammonite.ops.Path(java.nio.file.FileSystems.getDefault().getPath(path)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Width propogate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### undefined width propogate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class T1 extends Component{\n",
    "    val a = in UInt(8 bits)\n",
    "    val b = out UInt()\n",
    "    b := a\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  unsigned auto resize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class T2 extends Component{\n",
    "    val a = in UInt(8 bits)\n",
    "    val b = out UInt(10 bits)\n",
    "    b := a.resized\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  signed auto resize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class T3 extends Component{\n",
    "    val a = in SInt(8 bits)\n",
    "    val b = out SInt( )\n",
    "    b := a.resize(16)\n",
    "}\n",
    "showRtl(new T3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### width auto cut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class T4 extends Component{\n",
    "    val a = in SInt(8 bits)\n",
    "    val b = out SInt(4 bits)\n",
    "    b := a.resized\n",
    "}\n",
    "showRtl(new T4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class T4 extends Component{\n",
    "    val a = in SInt(8 bits)\n",
    "    val b = out Bool()\n",
    "    b := a.andR\n",
    "}\n",
    "showRtl(new T4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Width propogate cross boundary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Pass extends Component{\n",
    "    val a = in UInt() //without width declare\n",
    "    val b = out UInt() //without width declare\n",
    "    b := a\n",
    "}\n",
    "\n",
    "class Top extends Component{\n",
    "    val x = in UInt(8 bits)\n",
    "    val y = out UInt() //without width declare\n",
    "    \n",
    "    val uut = new Pass\n",
    "    uut.a := x\n",
    "    y     := uut.b\n",
    "}\n",
    "showRtl(new Top)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bit Join\n",
    "`##` return Bits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class T5 extends Component{\n",
    "    val a0 = in SInt(8 bits)    \n",
    "    val a1 = in SInt(8 bits)\n",
    "    val c = out SInt()\n",
    "    c:= (a0 ## a1).asSInt\n",
    "}\n",
    "showRtl(new T5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bits slice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "class T6 extends Component{\n",
    "    val a = in SInt(16 bits) \n",
    "    val b  = out SInt()\n",
    "    val c  = out SInt() \n",
    "    val o0,o1,o2,o3 = out Bool()\n",
    "    \n",
    "    b  := a(5 downto 2)  \n",
    "    c  := a(0 to 4)\n",
    "    o0 := a.msb\n",
    "    o1 := a.lsb\n",
    "    o2 := a(0)\n",
    "    o3 := a(9)\n",
    "    \n",
    "    println(c.getWidth)\n",
    "}\n",
    "\n",
    "showRtl(new T6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.12.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
